
import { Component, OnInit, ElementRef, AfterViewInit, ViewChild } from '@angular/core';
import { Http, Response } from '@angular/http';
import { DxDataGridComponent } from 'devextreme-angular';
import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';
declare const $: any;
import * as Enumerable from 'linq';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { QueryModel } from '../../model/report/query-model';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { DxTreeViewModule, DxListModule, DxTemplateModule } from 'devextreme-angular';
import { UserModel } from '../../model/sys/user-model';
import { Notifycation } from 'app/ptpmc/comm/notifycation';
import { DateUtils } from '../../model/comm/date-utils';
@Component({
    selector: 'app-report-role',
    templateUrl: './role.component.html',
    styleUrls: ['./role.component.scss']
})

export class RoleComponent implements OnInit {
    title = '系统角色管理';
    public roleList: any[] = [];
    dialogTitle = '角色管理';
    popupVisible = false;
    selectedItems: any[] = [];

    roleItem: { id, name, createBy: { id }, createDate, updateBy: { id }, updateDate, delFlag }
        = {
            id: '', name: '', createBy: { id: UserModel.userId },
            createDate: DateUtils.getNowFormatDate(), updateBy: { id: UserModel.userId },
            updateDate: DateUtils.getNowFormatDate(), delFlag: '0'
        };

    constructor(private commService: CommService) {
    }

    ngOnInit() {
        this.getReportData();
    }

    /**
     * 获取表格数据
     */
    getReportData(): void {
        this.commService
            .getList({}, ReportUrls.AllRoles)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.roleList = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    saveRoleItem() {
        this.commService
            .getList(this.roleItem, ReportUrls.PutRoleItem)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.getReportData();
                    this.popupVisible = false;
                }
                Notifycation.showNotification(model.resultCode === 200 ? '添加成功' : '添加失败', 'info');
            });
    }

    delRole() {
        if (this.selectedItems.length <= 0) {
            alert('请先选中要删除的角色');
            return;
        }
        if (confirm('是否确认删除选择的角色？')) {
            this.commService
                .getList(this.selectedItems, ReportUrls.DelRoleItem)
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        this.getReportData();
                        this.selectedItems = [];
                    }
                    Notifycation.showNotification(model.resultCode === 200 ? '删除成功' : '删除失败', 'info');
                });
        }
    }

    updateRoles() {
        this.roleList;
    }

    selectionChanged(data) {
        this.selectedItems = data.selectedRowsData;
    }

}
